#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UTable.Input.Keyboard;
using UTable.Objects.Controls;

namespace UTable.Objects
{
    internal class VirtualKeyboard: UObjectEntity, IKeyProvider
    {
        private VirtualKeyboardContent _content;

        public VirtualKeyboard(): base (new ObjectCreateParameter(typeof(VirtualKeyboardContent)))
        {

        }

        internal protected override void OnContentCreated(UObject content)
        {
            _content = content as VirtualKeyboardContent;
            _content.KeyDown += new KeyInputEventHandler(_content_KeyDown);
            _content.KeyUp += new KeyInputEventHandler(_content_KeyUp);
            _content.KeyPressed += new KeyInputEventHandler(_content_KeyPressed);
        }

        void _content_KeyPressed(KeyInputEventArgs args)
        {
            this.Dispatcher.BeginInvoke(new KeyInputEventHandler(OnKeyPressed), new object[] { args });
        }

        void _content_KeyUp(KeyInputEventArgs args)
        {
            this.Dispatcher.BeginInvoke(new KeyInputEventHandler(OnKeyUp), new object[] { args });
        }

        void _content_KeyDown(KeyInputEventArgs args)
        {
            this.Dispatcher.BeginInvoke(new KeyInputEventHandler(OnKeyDown), new object[] { args });
        }

        void OnKeyPressed(KeyInputEventArgs args)
        {
            if (this.KeyPressed != null)
                KeyPressed(args);
            if (this.InputTriggered != null)
            {
                InputTriggered(args);
            }
        }
        void OnKeyDown(KeyInputEventArgs args)
        {
            if (this.KeyDown != null)
                KeyDown(args);
            if (this.InputTriggered != null)
            {
                InputTriggered(args);
            }
        }
        void OnKeyUp(KeyInputEventArgs args)
        {
            if (this.KeyUp != null)
                KeyUp(args);
            if (this.InputTriggered != null)
            {
                InputTriggered(args);
            }
        }

        #region IKeyboard Members

        public ModifierKeys Modifiers
        {
            get
            {
                return _content.Keyboard.Modifiers;
            }
        }

        public IInputNode Target
        {
            get { return _content.Keyboard.Target; }
        }

        public IInputNode Focus(IInputNode element)
        {
            return (IInputNode)_content.Dispatcher.Invoke(new VirtualKeyboardContent.FocusHandler(_content.Focus), new object[] { element });
        }

        public KeyStates GetKeyStates(Key key)
        {
            return (KeyStates)_content.Dispatcher.Invoke(new VirtualKeyboardContent.GetKeyStatesHandler(_content.GetKeyStates), new object[] { key });
        }

        public bool IsKeyDown(Key key)
        {
            return (bool)_content.Dispatcher.Invoke(new VirtualKeyboardContent.CheckKeyStateHandler(_content.IsKeyDown), new object[] { key });
        }

        public bool IsKeyToggled(Key key)
        {
            return (bool)_content.Dispatcher.Invoke(new VirtualKeyboardContent.CheckKeyStateHandler(_content.IsKeyToggled), new object[] { key });
        }

        public bool IsKeyUp(Key key)
        {
            return (bool)_content.Dispatcher.Invoke(new VirtualKeyboardContent.CheckKeyStateHandler(_content.IsKeyUp), new object[] { key });
        }

        public new event KeyInputEventHandler KeyDown;

        public new event KeyInputEventHandler KeyUp;

        public event KeyInputEventHandler KeyPressed;

        #endregion

        #region IInputProvider Members

        public event UTable.Input.InputEventHandler InputTriggered;

        public void Initialize(UTable.Objects.UApplication context)
        {
        }

        #endregion
    }
}
